package tech.abing.spider.task.pipeline.amac;

import java.util.List;

import org.apache.log4j.Logger;

import tech.abing.spider.component.Pipeline;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.task.model.amac.CurriculumVitaeEntity;
import tech.abing.spider.task.model.amac.ExecutivesEntity;
import tech.abing.spider.task.model.amac.FundManagerEntity;
import tech.abing.spider.task.model.amac.FundStatEntity;
import tech.abing.spider.task.model.amac.ManagerStatEntity;
import tech.abing.spider.util.ProccessableUtil;
import tech.abing.spider.util.SqlUtil;

/**
 * 存储私募基金管理人信息
 * @author Ivan qb.huang@wescxx.com   
 * @date 2016-11-1 下午07:36:34 
 * @version V2.0
 */
public class ManagerPipeline implements Pipeline {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7422649491356327496L;
	private static Logger logger = Logger.getLogger(ManagerPipeline.class);
	private static SqlUtil sqlUtil = null;

	public ManagerPipeline() {
		if(sqlUtil == null){
			sqlUtil = SqlUtil.getInstance();
			logger.info("数据库连接创建成功！");
		}
	}
	
	@Override
	public void proccessData(List<Proccessable> objectContainer) {
		if(!objectContainer.isEmpty()){//由于在process层把三个实体类的数据都放在同一个容器里面，所以这里不能实行批处理方案
			for (Proccessable proccessable : objectContainer) {
				if(proccessable.getClass().getName().equals(FundManagerEntity.class.getName())){
					sqlUtil.insert(sqlUtil.getConn(), (FundManagerEntity)proccessable);
				}
				if(proccessable.getClass().getName().equals(CurriculumVitaeEntity.class.getName())){
					sqlUtil.insert(sqlUtil.getConn(), (CurriculumVitaeEntity)proccessable);
				}
				if(proccessable.getClass().getName().equals(ExecutivesEntity.class.getName())){
					sqlUtil.insert(sqlUtil.getConn(), (ExecutivesEntity)proccessable);
				}
				if(proccessable.getClass().getName().equals(ManagerStatEntity.class.getName())){
					sqlUtil.insert(sqlUtil.getConn(), (ManagerStatEntity)proccessable);
				}
			}
		}
		/*if (ProccessableUtil.instanceOfClass(objectContainer, FundManagerEntity.class)) {// 判断集合类型
			List<FundManagerEntity> list = ProccessableUtil.convert(objectContainer, FundManagerEntity.class);// 转换
			
			//sqlUtil.deleteForConn("delete from fund_manager");
			sqlUtil.batchInsert(sqlUtil.getConn(), FundManagerEntity.class, list);
		}*/
	}

	@Override
	public void destory() {
		if(sqlUtil != null){
			sqlUtil.closeConn();
			sqlUtil = null;
			logger.info("数据库连接关闭成功！");
		}
	}

}
